// 登录业务
const btnLogin = document.querySelector('#btn-login')
const form = document.querySelector('.login-form')
btnLogin.addEventListener('click', async function () {


    const data = serialize(form, { hash: true, empty: true })
    const { username, password } = data
    if (username.length < 8 || username.length > 30) {
        return showToast('用户名是8-30位')
    }
    if (password.length < 6 || password.length > 30) {
        return showToast('密码是6-30位')
    }
    //提交用户名和密码到服务器
    // const { data: { message } } = await axios.post('/register', data)
    console.log(data);

    try {
        const { message, data: { token } } = await axios({
            url: '/login',
            method: 'post',
            data: {
                username: data.username,
                password: data.password
            }

        })
        //提示
        showToast(message)
        //存储用户名
        localStorage.setItem('user', username)
        //存储令牌
        localStorage.setItem('token', token)
        //跳转到首页
        setTimeout(() => {
            location.href = 'index.html'
        }, 1500)
    } catch (err) {
        //查看后端返回的错误信息dir方法
        // console.dir(err);
        showToast(err.response.message)
    }
})